package gui;

import javax.swing.*;

import fiziksobject.EarthGravity;
import fiziksobject.FiziksBall;
import fiziksobject.Gravity;
import fiziksobject.Springs;
import fiziksobject.electricField;
import fiziksobject.magneticField;

import physics.Location;
import physics.PhysicsEngine;
import physics.Vector;
import physics.Vector.Type;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main {
	public static PhysicsSettings settings = new PhysicsSettings();
	public static FiziksWorld fw = new FiziksWorld(settings,new Location(0,0));
	public static JFrame j = new JFrame();
	public static JScrollPane jsp = new JScrollPane(fw);
	public static void main(String[] args) {
		j.setLayout(new BorderLayout());
		JPanel buttons = new JPanel();
		j.add(buttons, BorderLayout.SOUTH);
		JButton start = new JButton("Start");
		JButton pause = new JButton("Pause");
		JButton restart = new JButton("Restart");
		buttons.setLayout(new FlowLayout());
		buttons.add(start);
		buttons.add(pause);
		buttons.add(restart);
		j.add(jsp, BorderLayout.CENTER);
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Main.fw.start();
			}
		});
		pause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Main.fw.pause();
			}
		});
		restart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Main.fw.clearPhysicsObjects();
			}
		});
		
		JMenuBar bar = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenu physics = new JMenu("Physics");
		JMenu options = new JMenu("Options");
		JMenuItem load = new JMenuItem("Load");
		JMenuItem save = new JMenuItem("Save");
		JMenuItem quit = new JMenuItem("Quit");
		JMenuItem forcers = new JMenuItem("Forcers");
		JMenuItem physobjs = new JMenuItem("Physics Objects");
		JMenuItem setting = new JMenuItem("Settings");
		
		bar.add(file);
		bar.add(physics);
		bar.add(options);
		
		file.add(load);
		file.add(save);
		file.add(quit);
		physics.add(forcers);
		physics.add(physobjs);
		options.add(setting);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser f = new JFileChooser();
				f.showSaveDialog(j);
				File write = f.getSelectedFile();
				try {
					ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(write));
					oos.writeObject(settings);
					oos.writeObject(jsp);
					oos.writeObject(PhysicsEngine.getPhysicsEngine());
				} catch (Exception fgsfds) {
					fgsfds.printStackTrace();
				}
			}
		});
		load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser f = new JFileChooser();
				f.showOpenDialog(j);
				File write = f.getSelectedFile();
				try {
					ObjectInputStream ois = new ObjectInputStream(new FileInputStream(write));
					settings = (PhysicsSettings) ois.readObject();
					JScrollPane myjsp = (JScrollPane) ois.readObject();
					PhysicsEngine.setPhysicsEngine((PhysicsEngine)ois.readObject());
					j.remove(jsp);
					jsp = myjsp;
					j.add(jsp,BorderLayout.CENTER);
				} catch (Exception fgsfds) {
					fgsfds.printStackTrace();
				}
			}
		});
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				System.exit(0);
			}
		});
		forcers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				ForcerMenu fm = new ForcerMenu();
				fm.setVisible(true);
				fm.setSize(377,610);
				fm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		physobjs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				FiziksObjectMenu fom = new FiziksObjectMenu();
				fom.setVisible(true);
				fom.setSize(377,610);
				fom.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		setting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				SettingsWindow sw = new SettingsWindow(settings);
				sw.setSize(377,610);
				sw.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				sw.setVisible(true);
			}
		});
		
		j.setJMenuBar(bar);
		
		j.setSize(987, 610);
		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		j.setVisible(true);
		
		ForcerEditorManager.get().registerForcerEditor(Gravity.class, GravityEditor.class);
		ForcerEditorManager.get().registerForcerEditor(electricField.class, ElectricFieldEditor.class);
		ForcerEditorManager.get().registerForcerEditor(Springs.class, SpringProperties.class);
		ForcerEditorManager.get().registerForcerEditor(magneticField.class,MagneticFieldEditor.class);
		
		PhysicsEngine pe = PhysicsEngine.getPhysicsEngine();
//		electricField eF = new electricField(
//				new Vector(new Location(3.0, 4.0),null, 100, 100));
//		pe.addForcer(eF);

		//pe.addForcer(new magneticField(32000,false));
		//pe.addForcer(new Gravity(new Vector(null, null, 0,-9.8)));
	}
}
